package org.example.controller.shanjia;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.Shanpinfele;
import org.example.result.PageResult;
import org.example.result.Pages;
import org.example.result.Result;
import org.example.service.ShanpinfeleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/shanjia/shanpin")
@Slf4j
@Api(tags = "商品分类相关接口")
public class GlshanpinfeleController {
    @Autowired
    ShanpinfeleService shanpinfeleService;

    /**
     * 商品分类分页查询
     * @param pages
     * @return
     */
    @PostMapping("/fele")
    @ApiOperation(value = "商品分类查询")
    public Result<PageResult> felecx(@RequestBody Pages pages) {
        log.info("分页查询，参数为：{}", pages);
        PageResult pageResult = shanpinfeleService.pageQuery(pages);
        return Result.success(pageResult);
    }

    /**
     * 商品分类新增
     * @param shanpinfele
     * @return
     */
    @PostMapping("/felexz")
    @ApiOperation(value = "商品分类新增")
    public Result felexz(@RequestBody Shanpinfele shanpinfele) {
        log.info("新增分类：{}", shanpinfele);
       shanpinfeleService.felexz(shanpinfele);
        return Result.success();
    }

    /**
     * 商品分类批量删除
     * @param name
     * @return
     */
    @DeleteMapping ("/feleplsc")
    @ApiOperation(value = "商品分类批量删除")
    public Result feleplsc(@RequestParam( "name") String name,@RequestParam( "status") String status,@RequestParam( "username") String username) {
        log.info("批量删除分类：{}", name,status,username);
        if(shanpinfeleService.feleplsc(name,status,username)==1)
        return Result.success();
        return Result.error("失败");
    }

    /**
     * 商品分类查询
     * @param name
     * @return
     */
    @GetMapping ("/felecx")
    @ApiOperation(value = "商品分类查询")
    public Result feleplcx(@RequestParam( "name") String name,@RequestParam( "username") String username) {
        log.info("查询分类：{}", name);
        if(shanpinfeleService.feleplcx(name,username)==null) {
            return  Result.error("失败");
        }
        else{

            return Result.success();
        }
    }

    /**
     * 商品分类修改
     * @param shanpinfele
     * @return
     */
    @PutMapping ("/xg")
    @ApiOperation(value = "商品分类修改")
    public Result felexg(@RequestBody Shanpinfele shanpinfele) {
        log.info("修改分类：{}", shanpinfele);
        shanpinfeleService.felexg(shanpinfele);
        return Result.success();
    }

    /**
     * 商品分类修改
     * @param shanpinfele
     * @return
     */
    @PutMapping ("/zt")
    @ApiOperation(value = "商品分类状态")
    public Result felezt(@RequestBody Shanpinfele shanpinfele) {
        log.info("修改分类状态：{}", shanpinfele);
        shanpinfeleService.felezt(shanpinfele);
        return Result.success();
    }


    /**
     * 商品分类删除
     * @param
     * @return
     */
    @DeleteMapping ("/sc")
    @ApiOperation(value = "删除商品分类")
    public Result felezt(@RequestParam( "id") int id) {
        log.info("删除分类：{}", id);
        shanpinfeleService.felesc(id);
        return Result.success();
    }


    /**
     * 商品分类查询
     * @param
     * @return
     */
    @GetMapping ("/namecx")
    @ApiOperation(value = "商品分类name查询")
    public Result<String[]> feleplnamecx(@RequestParam( "username") String username) {
        log.info("name查询分类：{}", username);
        return Result.success( shanpinfeleService.feleplnamecx(username));
    }


}
